#include <stdio.h>
#include "sqlite3.h"
#include <stdlib.h>
#include "public.h"
#include <string.h>
#include <arpa/inet.h>
#include <math.h>
#include "cJSON.h"

const char *dbPathName = "/opt/dhdb-v01.db";
#define POLLPACKETMAXLEN 100
#define JXDEVDATAMAXLEN 20
#define GENERAL_SQL_MAXLEN 20
#define MB_LONGDATALIST_LEN 20000
#define JX_LONGDATALIST_LEN 40000

const *linkageControlSwitchName = "联动控制开关";
LINKAGE_CONTROL g_linkageControl;
devDataTable *g_devDataTab;
jxDevDataTab *g_jxDevDataTab;
JXLINKAGETAB *g_jxLinkageTab;
RS485CFG *g_RS485Cfg;
MBPOLLCFG *g_mbPollTab;
char g_mbLongDataList[MB_LONGDATALIST_LEN];
char g_jxDevLongDataList[JX_LONGDATALIST_LEN];
PACKET_record *g_CommPacket;
nonStdMbCmdTab *g_nonStdMbCmdPacket;

char *g_mqComVer = NULL;
char g_mqComId[50] = {0};
char g_mqTopicReport[50] = {0};
char g_mqTopicCtrl[50] = {0};
char g_mqServer[30] = {0};
char g_mqClientId[50] = {0};
INT16U g_tabLen = 0;
INT16U g_jxTabLen = 0;
INT16U g_jxLinkageTabLen = 0;
INT16U g_RS485CfgLen = 0;
INT16U g_mbPollTabLen = 0;
INT16U g_nonStdMbCmdTabLen = 0;

INPUTCfg g_IOinput[IO_DI_MAXLEN];
OUTPUTCfg g_IOoutput[IO_DO_MAXLEN];

extern INT8U g_temp[];

INT16U g_comPackeIdx;
static void getJxDevDataList(void);
static void getJxDevData(void);
static void getDevInfo(void);
static void getRS485Cfg(void);
static void getMbPollCfg(void);
static void getNonStdCmd(void);
static void g_commPacketSortByRs485(void);
static void getInputCfg(void);
static void getOutputCfg(void);
static void SortByNonStdCmd(void);
static void SortByRs485(void);
static void SortByMbDevAddr(void);
static void SortByMbDevReadCmd(void);
static void SortByMbStartAddr(void);
static void g_commPacketForm(void);
static void sql_generalGet(char *strSql);
static void getJxLinkageCfg(void);
void PollSpecial_PacketTreat(INT16U starAddrIdx, INT16U len);
void PollBits_PacketTreat(INT16U starAddrIdx, INT16U len);
void PollWords_PacketTreat(INT16U starAddrIdx, INT16U len);
char *jxJsonForm(char *key, char *value);
char *generalJsonForm(char *key1, char *value1, char *key2, char *value2);
int sql_general_interface(char *strSql, char *recvbuf);
// int sql_general_set_interface(char *strSql, char *recvbuf);
int sql_general_set_interface(char *strSql);
//--------------------------------------------------------------------
void sqTable_init(void)
{

	//char *strSql = "select name from sqlite_master where type='table' ORDER BY name";
	//  char *strSql = "update CfgTab_devInfo set content ='jxdh01222' where cfgName=devName;";
	// char *strSql ="update jxSensorId set sensorCnName='温湿度传感器2' where sensorId =1";
	//char *strSql ="update jxSensorId set sensorCnName='111#温湿度传感器', sensorEnName='tempHub11111', active='1', modifyEnb='1' where sensorId =1";
	//sql_general_set_interface(strSql,&g_temp);


	// sql_generalGet(strSql);
	//getJxDevDataList();
	getJxDevData();
	getDevInfo();
	getNonStdCmd();
	getMbPollCfg();
	getRS485Cfg();
	getInputCfg();
	getOutputCfg();
	getJxLinkageCfg();
}
static int callback(void *NotUsed, int argc, char **argv, char **azColName)
{
   int i;
   //printf("\n333333333333333333333333333333\n");
   for(i=0; i<argc; i++)
   {
      DebugPrint("%s = %s\n", azColName[i], argv[i] ? argv[i] : "NULL");
   }
   DebugPrint("\n");
   return 0;
}
// int sql_general_set_interface(char *strSql, char *recvbuf)
int sql_general_set_interface(char *strSql)
{
	char **pResult;
	int nRow, nCol, nResult;
	char *errmsg;
	int i, j;
	int m = 0;

	DebugPrint("\nstrSql=%s\n", strSql);
	sqlite3 *pdb = NULL;
	nResult = sqlite3_open_v2(dbPathName, &pdb, SQLITE_OPEN_READWRITE, NULL);
    if (nResult != SQLITE_OK)goto end;
	//nResult = sqlite3_exec(pdb, strSql, callback, 0, &errmsg);
	nResult = sqlite3_exec(pdb, strSql, 0, 0, &errmsg);
	if (nResult != SQLITE_OK)
	{
		DebugPrint("sql_general_set_interface err\n");
		goto end;
	}
	else
	{
		m++;
		DebugPrint("sql_general_set_interface ok\n");
	}
end:
	sqlite3_close(pdb);
	sqlite3_free(errmsg);
	//exit(EXIT_FAILURE);
	return m;
}
int sql_general_interface(char *strSql, char *recvbuf)
{
	char **pResult;
	int nRow, nCol, nResult;
	char *errmsg;
	int i, j;

	int m = 0;
	int index = 0;

	DebugPrint("\nstrSql=%s\n", strSql);
	sqlite3 *pdb = NULL;
	nResult = sqlite3_open_v2(dbPathName, &pdb, SQLITE_OPEN_READWRITE, NULL);
	nResult = sqlite3_get_table(pdb, strSql, &pResult, &nRow, &nCol, &errmsg);
	if (nResult != SQLITE_OK)
	{
		DebugPrint("sql read err\n");
		goto end;
	}

	recvbuf[m++] = nRow;
	recvbuf[m++] = nRow >> 8;
	recvbuf[m++] = nCol;
	recvbuf[m++] = nCol >> 8;

	for (i = 0; i < nRow + 1; i++)
	{
		for (j = 0; j < nCol; j++)
		{
			if (pResult[index + j] != NULL)
			{
				strcpy(&recvbuf[m], pResult[index + j]);
				m += strlen(pResult[index + j]);
				recvbuf[m++] = 0;
			}
			else
			{
				recvbuf[m++] = 0;
			}
		}

		index += nCol;
	}

	for (i = 0; i < m; i++)
	{
		if (recvbuf[i] == 0)
		{
			DebugPrint(" | ");
		}
		else
		{
			DebugPrint("%c", recvbuf[i]);
		}
	}
	// printf("m=%d\n",m);

//  printf("len=%d;pResult=%s\n",m,pResult);
end:
	sqlite3_close(pdb);
	sqlite3_free(errmsg);
	return m;
}
static void sql_generalGet(char *strSql)
{
	char **pResult;
	int nRow, nCol, nResult;
	char *errmsg;
	int i, j;

	sqlite3 *pdb = NULL;
	nResult = sqlite3_open_v2(dbPathName, &pdb, SQLITE_OPEN_READWRITE, NULL);

	nResult = sqlite3_get_table(pdb, strSql, &pResult, &nRow, &nCol, &errmsg);
	if (nResult != SQLITE_OK)
		goto end;

	int index = 0;
	g_temp[0] = 0;
	strcat(g_temp, gs_siteId);
	strcat(g_temp, ",");
	char temp[10];
	sprintf(temp, "%d ", nRow);
	strcat(g_temp, temp);
	strcat(g_temp, ",");
	sprintf(temp, "%d ", nCol);
	strcat(g_temp, temp);
	strcat(g_temp, ",");
	for (i = 0; i < nRow; i++)
	{
		for (j = 0; j < nCol; j++)
		{
			strcat(g_temp, pResult[index++]);
			strcat(g_temp, ",");
		}
	}
	char *p = generalJsonForm("sqlRequest", strSql, "sqlResponse", g_temp);
	pthread_mutex_lock(&comBuff0.lock);
	AP_circleBuff_WritePacket(p, strlen(p), DTU2MQTPB);
	pthread_mutex_unlock(&comBuff0.lock);

end:
	sqlite3_close(pdb);
	sqlite3_free(errmsg);
	//exit(EXIT_FAILURE);
}
static void getJxDevData(void)
{
	sqlite3 *pdb = NULL;
	char **pResult;
	int nRow, nCol, nResult;
	char *errmsg;
	int i;
	// printf("dbPathName=%s ", dbPathName);
	nResult = sqlite3_open_v2(dbPathName, &pdb, SQLITE_OPEN_READWRITE, NULL);
	if (nResult)
	{
		fprintf(stderr, "can't open database:%s\n", sqlite3_errmsg(pdb));
		sqlite3_close(pdb);
		exit(EXIT_FAILURE);
	}
	char *strSql = "select * from jxDevData where enb!=0";
	nResult = sqlite3_get_table(pdb, strSql, &pResult, &nRow, &nCol, &errmsg);
	if (nResult != SQLITE_OK)
		goto end;
	int nIndex = nCol;

	g_jxTabLen = nRow;

	if ((g_jxDevDataTab = (jxDevDataTab *)malloc(g_jxTabLen * sizeof(jxDevDataTab))) == NULL)
	{
		DebugPrint("malloc g_jxDevDataTab error\n");
		exit(EXIT_FAILURE);
	}
	int g_jxDevLongDataNextAddr = 0;
	for (i = 0; i < g_jxTabLen; i++)
	{
		g_jxDevDataTab[i].oid = atoi(pResult[nIndex]);
		g_jxDevDataTab[i].oidLen = atoi(pResult[nIndex + 1]);
		g_jxDevDataTab[i].oidLen+=5;
		strcpy(g_jxDevDataTab[i].oidName, pResult[nIndex + 2]);
		g_jxDevDataTab[i].sensorId = atoi(pResult[nIndex + 3]);
		g_jxDevDataTab[i].devValInt = atoi(pResult[nIndex + 4]);

		g_jxDevDataTab[i].paraType = atoi(pResult[nIndex + 5]);
		g_jxDevDataTab[i].radio = atoi(pResult[nIndex + 6]);
		g_jxDevDataTab[i].enb = atoi(pResult[nIndex + 9]);
		
		g_jxDevDataTab[i].longDataAtListAddr = 0;
		nIndex = nIndex + nCol;

		if (g_jxDevDataTab[i].oidLen > 9)
		{
			g_jxDevDataTab[i].longDataAtListAddr = g_jxDevLongDataNextAddr;
			g_jxDevLongDataNextAddr += g_jxDevDataTab[i].oidLen;
			if (g_jxDevLongDataNextAddr > JX_LONGDATALIST_LEN)
			{
				DebugPrint("g_jxDevLongDataNextAddr over Assignment %d\n", JX_LONGDATALIST_LEN);
				g_jxDevDataTab[i].oidLen = 9;
			}
		}
		g_jxDevDataTab[i].sqlValue = g_jxDevDataTab[i].devValInt;
		if (!strcmp(g_jxDevDataTab[i].oidName, linkageControlSwitchName))
		{
			g_linkageControl.oid = g_jxDevDataTab[i].oid;
			g_linkageControl.writeFlag = 0;
			g_linkageControl.value = (g_jxDevDataTab[i].devValInt == 0) ? 0 : 1;

			// printf("\ng_jxDevDataTab[i].oidName=%s \n", g_jxDevDataTab[i].oidName);
			// printf("\ng_OidlinkageControlSwitch=%d \n", g_OidlinkageControlSwitch);
		}
	}

	for (i = 0; i < g_jxTabLen; i++)
	{
		// printf("---------------------------------\n ");
		printf("g_jxDevDataTab[i].oid=%d ", g_jxDevDataTab[i].oid);
		printf("oidLen=%d ", g_jxDevDataTab[i].oidLen);
		printf("oidName=%s ", g_jxDevDataTab[i].oidName);
		printf("sensorId=%d ", g_jxDevDataTab[i].sensorId);
		printf("valueInt=%d ", g_jxDevDataTab[i].devValInt);
		printf("paraType=%d ", g_jxDevDataTab[i].paraType);
		printf("radio=%d ", g_jxDevDataTab[i].radio);
		printf("longDataAtListAddr=%d ", g_jxDevDataTab[i].longDataAtListAddr);
		printf("enb=%d ", g_jxDevDataTab[i].enb);

		printf("\n ");
	}
end:
	sqlite3_close(pdb);
	sqlite3_free(errmsg);
}
static void getJxDevDataList(void)
{
	sqlite3 *pdb = NULL;
	char **pResult;
	int nRow, nCol, nResult;
	char *errmsg;
	int i;

	nResult = sqlite3_open_v2(dbPathName, &pdb, SQLITE_OPEN_READWRITE, NULL);
	if (nResult)
	{
		fprintf(stderr, "can't open database:%s\n", sqlite3_errmsg(pdb));
		sqlite3_close(pdb);
		exit(EXIT_FAILURE);
	}
	char *strSql = "select * from jxDevData";
	nResult = sqlite3_get_table(pdb, strSql, &pResult, &nRow, &nCol, &errmsg);
	if (nResult != SQLITE_OK)
	{
		sqlite3_close(pdb);
		sqlite3_free(errmsg);
		return;
	}

	int index = 0;
	int j;
	int nColumn = nCol;

	g_jxTabLen = nRow;

	if ((g_jxDevDataTab = (jxDevDataTab *)malloc(g_jxTabLen * sizeof(jxDevDataTab))) == NULL)
	{
		DebugPrint("malloc g_jxDevDataTab error\n");
		exit(EXIT_FAILURE);
	}

	int comPkSUM = ceil((float)(nRow + 1) / JXDEVDATAMAXLEN);
	DebugPrint("comPkSUM=%d\n", comPkSUM);

	int m, n;
	n = 0;
	for (m = 0; m < comPkSUM; m++)
	{
		g_temp[0] = 0;
		strcat(g_temp, gs_siteId);
		strcat(g_temp, ",");
		char temp[10];
		sprintf(temp, "%d ", nRow);
		strcat(g_temp, temp);
		strcat(g_temp, ",");
		sprintf(temp, "%d ", nColumn);
		strcat(g_temp, temp);
		strcat(g_temp, ",");
		sprintf(temp, "%d ", comPkSUM);
		strcat(g_temp, temp);
		strcat(g_temp, ",");
		sprintf(temp, "%d ", m + 1);
		strcat(g_temp, temp);
		strcat(g_temp, ",");
		for (i = 0; i < JXDEVDATAMAXLEN; i++)
		{
			for (j = 0; j < nColumn; j++)
			{
				strcat(g_temp, pResult[index++]);
				strcat(g_temp, ",");
			}
			if (++n > nRow)
				break;
		}
		char *p = jxJsonForm("jxDevDataList", g_temp);
		pthread_mutex_lock(&comBuff0.lock);
		AP_circleBuff_WritePacket(p, strlen(p), DTU2MQTPB);
		pthread_mutex_unlock(&comBuff0.lock);
	}
}

static void getDevInfo(void)
{
	sqlite3 *pdb = NULL;
	char **pResult;
	int nRow, nCol, nResult;
	char *errmsg;

	nResult = sqlite3_open_v2(dbPathName, &pdb, SQLITE_OPEN_READWRITE, NULL);
	if (nResult)
	{
		fprintf(stderr, "can't open database:%s\n", sqlite3_errmsg(pdb));
		sqlite3_close(pdb);
		exit(EXIT_FAILURE);
	}
	//----------------select CfgTab_devInfo-----------------------------------------------------
	char *strSql = "select content from CfgTab_devInfo WHERE cfgName = 'mqComVer'";
	nResult = sqlite3_get_table(pdb, strSql, &pResult, &nRow, &nCol, &errmsg);
	if (nResult != SQLITE_OK)
	{
		goto end;
	}
	g_mqComVer = (char *)pResult[nCol];
	DebugPrint("g_mqComVer =%s\n", g_mqComVer);

	strSql = "select content from CfgTab_devInfo WHERE cfgName = 'coLtdId'";
	nResult = sqlite3_get_table(pdb, strSql, &pResult, &nRow, &nCol, &errmsg);
	if (nResult != SQLITE_OK)
	{
		goto end;
	}
	char *g_coLtdId = (char *)pResult[nCol];
	DebugPrint("g_coLtdIdr =%s\n", g_coLtdId);

	strSql = "select content from CfgTab_devInfo WHERE cfgName = 'transRoomId'";
	nResult = sqlite3_get_table(pdb, strSql, &pResult, &nRow, &nCol, &errmsg);
	if (nResult != SQLITE_OK)
	{
		goto end;
	}
	char *g_transRoomId = (char *)pResult[nCol];
	DebugPrint("g_transRoomId =%s\n", g_transRoomId);

	strSql = "select content from CfgTab_devInfo WHERE cfgName = 'mqServer'";
	nResult = sqlite3_get_table(pdb, strSql, &pResult, &nRow, &nCol, &errmsg);
	if (nResult != SQLITE_OK)
	{
		goto end;
	}
	strcat(g_mqServer, pResult[nCol]);
	//g_mqServer = pResult[nCol];
	DebugPrint("g_mqServer =%s\n", g_mqServer);

	strSql = "select content from CfgTab_devInfo WHERE cfgName = 'clentId'";
	nResult = sqlite3_get_table(pdb, strSql, &pResult, &nRow, &nCol, &errmsg);
	if (nResult != SQLITE_OK)
	{
		goto end;
	}
	strcat(g_mqClientId, pResult[nCol]);
	//	g_mqClientId =pResult[nCol];
	DebugPrint("g_mqClientId =%s\n", g_mqClientId);

	//g_mqComId =g_coLtdId;
	strcat(g_mqComId, g_coLtdId);
	strcat(g_mqComId, ".");
	strcat(g_mqComId, g_transRoomId);
	strcat(g_mqComId, ".");

	strcat(g_mqTopicReport, g_coLtdId);
	strcat(g_mqTopicReport, "/");
	strcat(g_mqTopicReport, g_transRoomId);
	strcat(g_mqTopicReport, "/report");

	strcat(g_mqTopicCtrl, g_coLtdId);
	strcat(g_mqTopicCtrl, "/");
	strcat(g_mqTopicCtrl, g_transRoomId);
	strcat(g_mqTopicCtrl, "/control");

	DebugPrint("g_mqComId =%s\n", g_mqComId);
	DebugPrint("g_mqTopicReport =%s\n", g_mqTopicReport);
	DebugPrint("g_mqTopicCtrl =%s\n", g_mqTopicCtrl);
	//---------------------------------------------------------------------

end:
	sqlite3_close(pdb);
	sqlite3_free(errmsg);
}

static void getNonStdCmd(void)
{
	char **pResult;
	int nRow, nCol, nResult;
	char *errmsg;
	int i, j;

	sqlite3 *pdb = NULL;
	nResult = sqlite3_open_v2(dbPathName, &pdb, SQLITE_OPEN_READWRITE, NULL);

	char *strSql = "select * from CfgTab_nonStdMbCmd";
	nResult = sqlite3_get_table(pdb, strSql, &pResult, &nRow, &nCol, &errmsg);
	if (nResult != SQLITE_OK)
	{
		sqlite3_close(pdb);
		sqlite3_free(errmsg);
		return;
	}

	int nIndex = nCol;
	g_nonStdMbCmdTabLen = nRow;

	if ((g_nonStdMbCmdPacket = (nonStdMbCmdTab *)malloc((g_nonStdMbCmdTabLen) * sizeof(nonStdMbCmdTab))) == NULL)
	{
		DebugPrint("malloc g_nonStdMbCmdPacket error\n");
		exit(EXIT_FAILURE);
	}

	for (i = 0; i < g_nonStdMbCmdTabLen; i++)
	{
		g_nonStdMbCmdPacket[i].id = atoi(pResult[nIndex]);

		strcpy(g_temp, pResult[nIndex + 1]);
		rmZfFun(g_temp, 0x20);
		g_nonStdMbCmdPacket[i].sendLen = strlen(g_temp) / 2;
		if (strlen(g_temp) % 2 == 0 && g_nonStdMbCmdPacket[i].sendLen < 20)
		{
			StrToHex(&g_nonStdMbCmdPacket[i].ctx[0], g_temp, strlen(g_temp));
		}
		else
		{
			g_nonStdMbCmdPacket[i].sendLen = 0;
		}
		g_nonStdMbCmdPacket[i].recLen = atoi(pResult[nIndex + 2]);
		g_nonStdMbCmdPacket[i].paraGetStartAddr = atoi(pResult[nIndex + 3]);
		nIndex = nIndex + nCol;
	}
	//-------------------------------------------------
	for (i = 0; i < g_nonStdMbCmdTabLen; i++)
	{
		printf("---------------------------------\n");
		printf("nonCmdId=%d; sendLen=%d;recLen=%d;data=", g_nonStdMbCmdPacket[i].id, g_nonStdMbCmdPacket[i].sendLen, g_nonStdMbCmdPacket[i].recLen);
		for (j = 0; j < g_nonStdMbCmdPacket[i].sendLen; j++)
		{
			printf("%d ", g_nonStdMbCmdPacket[i].ctx[j]);
		}
		printf("\n");
	}
	//-------------------------------------------------
	sqlite3_close(pdb);
	sqlite3_free(errmsg);
}
static void getRS485Cfg(void)
{
	char **pResult;
	int nRow, nCol, nResult;
	char *errmsg;
	int i;

	sqlite3 *pdb = NULL;
	nResult = sqlite3_open_v2(dbPathName, &pdb, SQLITE_OPEN_READWRITE, NULL);

	char *strSql = "select * from CfgTab_RS485";
	nResult = sqlite3_get_table(pdb, strSql, &pResult, &nRow, &nCol, &errmsg);
	if (nResult != SQLITE_OK)
	{
		sqlite3_close(pdb);
		sqlite3_free(errmsg);
		return;
	}

	int nIndex = nCol;
	g_RS485CfgLen = nRow;
	//---------------------------------------------------

	DebugPrint("g_RS485CfgLen=%d\n", g_RS485CfgLen);
	if ((g_RS485Cfg = (RS485CFG *)malloc((g_RS485CfgLen) * sizeof(RS485CFG))) == NULL)
	{
		DebugPrint("malloc g_RS485Cfg error\n");
		exit(EXIT_FAILURE);
	}

	for (i = 0; i < g_RS485CfgLen; i++)
	{
		strcpy(g_RS485Cfg[i].name, pResult[nIndex]);
		g_RS485Cfg[i].speed = atoi(pResult[nIndex + 1]);
		g_RS485Cfg[i].databits = atoi(pResult[nIndex + 2]);
		g_RS485Cfg[i].stopbits = atoi(pResult[nIndex + 3]);
		g_RS485Cfg[i].parity = atoi(pResult[nIndex + 4]);
		g_RS485Cfg[i].option = atoi(pResult[nIndex + 5]);
		nIndex = nIndex + nCol;
	}
	sqlite3_close(pdb);
	sqlite3_free(errmsg);
}
static int getMbOidLen(INT32U oid, INT8U cmd)
{
	int len = get_jxOidParaLen(oid);
	if (len > 6)
	{
		len -= 5;
	}
	else if (cmd == 0x01 && cmd == 0x02)
	{
		len = 1;
	}
	else if (cmd == 0x03 && cmd == 0x04)
	{
		len = 2;
	}
	else
		len = 0;
	return len;
}
static void getMbPollCfg(void)
{
	char **pResult;
	int nRow, nCol, nResult;
	char *errmsg;
	int i;

	sqlite3 *pdb = NULL;
	nResult = sqlite3_open_v2(dbPathName, &pdb, SQLITE_OPEN_READWRITE, NULL);

	char *strSql = "select * from CfgTab_mbPoll";
	nResult = sqlite3_get_table(pdb, strSql, &pResult, &nRow, &nCol, &errmsg);
	if (nResult != SQLITE_OK)
	{
		sqlite3_close(pdb);
		sqlite3_free(errmsg);
		return;
	}

	int nIndex = nCol;
	g_mbPollTabLen = nRow;
	//---------------------------------------------------

	if ((g_mbPollTab = (MBPOLLCFG *)malloc(g_mbPollTabLen * sizeof(MBPOLLCFG))) == NULL)
	{
		DebugPrint("malloc g_mbPollTab error\n");
		sleep(1);
		exit(EXIT_FAILURE);
	}
	// DebugPrint("000000000000000000000000000  g_mbPollTabLen=%d\n", g_mbPollTabLen);
	int g_mbLongDataNextAddr = 0;
	for (i = 0; i < g_mbPollTabLen; i++)
	{
		g_mbPollTab[i].oid = atoi(pResult[nIndex]);

		g_mbPollTab[i].pollPortNameIdx = changRs485Idx(pResult[nIndex + 2]);
		g_mbPollTab[i].mbDevAddr = atoi(pResult[nIndex + 3]);
		g_mbPollTab[i].stdMbReadCmd = atoi(pResult[nIndex + 4]);
		g_mbPollTab[i].stdMbSetCmd = atoi(pResult[nIndex + 5]);
		g_mbPollTab[i].mbStartAddr = atoi(pResult[nIndex + 6]);
		g_mbPollTab[i].nonStdCmdId = atoi(pResult[nIndex + 7]);
		g_mbPollTab[i].rwFlag = atoi(pResult[nIndex + 8]);
		g_mbPollTab[i].multiValue = atoi(pResult[nIndex + 9]);
		g_mbPollTab[i].devideValue = atoi(pResult[nIndex + 10]);
		g_mbPollTab[i].addSubValue = atoi(pResult[nIndex + 11]);

		if (g_mbPollTab[i].multiValue == 0)
			g_mbPollTab[i].multiValue = 1;
		if (g_mbPollTab[i].devideValue == 0)
			g_mbPollTab[i].devideValue = 1;
		g_mbPollTab[i].adjustFlag = 0;
		if (g_mbPollTab[i].multiValue != 1)
			g_mbPollTab[i].adjustFlag |= 1;
		if (g_mbPollTab[i].devideValue != 1)
			g_mbPollTab[i].adjustFlag |= 2;
		if (g_mbPollTab[i].addSubValue != 0)
			g_mbPollTab[i].adjustFlag |= 4;

		// printf("g_mbPollTab[i].multiValue= %d ", g_mbPollTab[i].multiValue);
		// printf("devideValue= %d ", g_mbPollTab[i].devideValue);
		// printf("addSubValue= %d ", g_mbPollTab[i].addSubValue);
		// printf(".adjustFlag= %d ", g_mbPollTab[i].adjustFlag);
		// INT16S test=100;
		// INT16U temp=(INT16U)((INT16S)test+g_mbPollTab[i].addSubValue);
		// printf("temp= %d\n", temp);

		g_mbPollTab[i].len = getMbOidLen(g_mbPollTab[i].oid, g_mbPollTab[i].stdMbReadCmd);
		g_mbPollTab[i].devValue[0] = 0;
		g_mbPollTab[i].devValue[1] = 0;
		g_mbPollTab[i].devValue[2] = 0;
		g_mbPollTab[i].devValue[3] = 0;
		g_mbPollTab[i].serValue[0] = 0;
		g_mbPollTab[i].serValue[1] = 0;
		g_mbPollTab[i].serValue[2] = 0;
		g_mbPollTab[i].serValue[3] = 0;
		g_mbPollTab[i].option = 0;
		g_mbPollTab[i].mbLongDataAtListStartAddr = 0;
		nIndex = nIndex + nCol;
		if (g_mbPollTab[i].len > 2)
		{
			g_mbPollTab[i].mbLongDataAtListStartAddr = g_mbLongDataNextAddr;
			g_mbLongDataNextAddr += g_mbPollTab[i].len;
			if (g_mbLongDataNextAddr > MB_LONGDATALIST_LEN)
			{
				DebugPrint("g_mbLongDataNextAddr over Assignment %d\n", MB_LONGDATALIST_LEN);
				g_mbPollTab[i].len = 2;
			}
		}
	}

	SortByNonStdCmd();
	SortByRs485();
	SortByMbDevAddr();
	SortByMbDevReadCmd();
	SortByMbStartAddr();
	g_commPacketForm();
	g_commPacketSortByRs485();
	for (i = 0; i < g_mbPollTabLen; i++)
	{

		printf("rs485[%d]", g_mbPollTab[i].pollPortNameIdx);
		printf("mbDevAddr=%d ", g_mbPollTab[i].mbDevAddr);
		printf("comPacketIndex=%d ", g_mbPollTab[i].comPacketIndex);
		printf("byteIndex=%d ", g_mbPollTab[i].byteIndex);
		printf("bitIndex=%d ", g_mbPollTab[i].bitIndex);
		printf("stdMbReadCmd=%d ", g_mbPollTab[i].stdMbReadCmd);
		printf("mbStartAddr=%d ", g_mbPollTab[i].mbStartAddr);
		printf("oid=%d ", g_mbPollTab[i].oid);
		printf("len=%d ", g_mbPollTab[i].len);
		printf("stdMbSetCmd=%d ", g_mbPollTab[i].stdMbSetCmd);
		printf("nonStdCmdId=%d ", g_mbPollTab[i].nonStdCmdId);
		printf("rwFlag=%d ", g_mbPollTab[i].rwFlag);
		printf("mbLongDataStartAddr=%d ", g_mbPollTab[i].mbLongDataAtListStartAddr);
		printf("\n ");
	}
	sqlite3_close(pdb);
	sqlite3_free(errmsg);
}
int changRs485Idx(char *p)
{
	INT16U i;
	char *rs485Name[] = { "RS485-1", "RS485-2", "RS485-3", "RS485-4", "RS485-5","RS485-6",};

	for (i = 0; i < 6; i++)
	{
		if (!strcmp(p, rs485Name[i]))
			return i;
	}
	return 0;
}
static void g_commPacketSortByRs485(void)
{
	INT16U i, j;
	PACKET_record temp;

	for (i = 0; i < g_comPackeIdx; i++)
	{
		for (j = 0; j < g_comPackeIdx - i - 1; j++)
		{
			if (g_CommPacket[j].portIdx > g_CommPacket[j + 1].portIdx)
			{
				temp = g_CommPacket[j];
				g_CommPacket[j] = g_CommPacket[j + 1];
				g_CommPacket[j + 1] = temp;
			}
		}
	}
	printf("3--------g_comPackeIdx=%d-----------------------------------------\n", g_comPackeIdx);
	for (i = 0; i < g_mbPollTabLen; i++)
	{
		if (g_CommPacket[i].packetInex == 0xff)
			break;
		printf("g_index=%d ", g_CommPacket[i].packetInex);
		printf("portIdx=%d ", g_CommPacket[i].portIdx);
		printf("starAddrIndex=%d ", g_CommPacket[i].starAddrIndex);
		printf("spCmdId=%d ", g_CommPacket[i].spCmdId);
		printf("recLen=%d ", g_CommPacket[i].recLen);
		printf("sendLen=%d ", g_CommPacket[i].sendLen);
		printf("devAddr=%x ", g_CommPacket[i].content.devAddr);
		printf("cmd=%x ", g_CommPacket[i].content.cmd);
		printf("starMbAddr=%x ", g_CommPacket[i].content.starMbAddr);
		printf("byteSum=%x ", g_CommPacket[i].content.byteSum);
		printf("crc=%x \n", g_CommPacket[i].content.crc);
	}
	printf("4-------------------------------------------------\n");
}
static void SortByNonStdCmd(void)
{
	INT16U i, j;
	MBPOLLCFG temp;

	for (i = 0; i < g_mbPollTabLen; i++)
	{
		for (j = 0; j < g_mbPollTabLen - i - 1; j++)
		{
			if (g_mbPollTab[j].nonStdCmdId > g_mbPollTab[j + 1].nonStdCmdId)
			{
				temp = g_mbPollTab[j];
				g_mbPollTab[j] = g_mbPollTab[j + 1];
				g_mbPollTab[j + 1] = temp;
			}
		}
	}
}
static void SortByRs485(void)
{
	INT16U m, i, j;
	MBPOLLCFG temp;
	INT16U curStartAddr = 0;
	INT16U nxStartAddr = 0;
	INT16U cnt = 0, sortFlag;
	lWord4Byte curType, nxType;

	for (i = 0; i < g_mbPollTabLen; i++)
	{
		sortFlag = 0;
		curType.lword = 0;
		nxType.lword = 0;

		curType.byte[0] = g_mbPollTab[i].nonStdCmdId;
		if (i <= g_mbPollTabLen - 1)
		{
			nxType.byte[0] = g_mbPollTab[i + 1].nonStdCmdId;
		}
		if (curType.lword != nxType.lword)
		{
			nxStartAddr = i + 1;
			cnt = i - curStartAddr + 1;
			sortFlag = 1;
		}

		if (sortFlag)
		{
			for (m = 0; m < cnt - 1; m++)
			{
				for (j = curStartAddr; j < cnt - 1 - m + curStartAddr; j++)
				{
					if (g_mbPollTab[j].pollPortNameIdx > g_mbPollTab[j + 1].pollPortNameIdx)
					{
						temp = g_mbPollTab[j];
						g_mbPollTab[j] = g_mbPollTab[j + 1];
						g_mbPollTab[j + 1] = temp;
					}
				}
			}
		}
		curStartAddr = nxStartAddr;
	}

	for (m = 0; m < g_mbPollTabLen; m++)
	{
		if (g_mbPollTab[m].nonStdCmdId != 0)
			break;
	}
	for (i = m; i < g_mbPollTabLen; i++)
	{
		for (j = 0; j < g_mbPollTabLen - i - 1; j++)
		{
			if (g_mbPollTab[j + m].pollPortNameIdx > g_mbPollTab[j + 1 + m].pollPortNameIdx)
			{
				temp = g_mbPollTab[j + m];
				g_mbPollTab[j + m] = g_mbPollTab[j + 1 + m];
				g_mbPollTab[j + 1 + m] = temp;
			}
		}
	}
}
static void SortByMbDevReadCmd(void)
{
	INT16U m, i, j;
	MBPOLLCFG temp;
	INT16U curStartAddr = 0;
	INT16U nxStartAddr = 0;
	INT16U cnt = 0, sortFlag;

	lWord4Byte curType, nxType;

	for (i = 0; i < g_mbPollTabLen; i++)
	{
		sortFlag = 0;

		curType.lword = 0;
		nxType.lword = 0;
		curType.byte[0] = g_mbPollTab[i].nonStdCmdId;
		curType.byte[1] = g_mbPollTab[i].pollPortNameIdx;
		curType.byte[2] = g_mbPollTab[i].mbDevAddr;

		if (i <= g_mbPollTabLen - 1)
		{
			nxType.byte[0] = g_mbPollTab[i + 1].nonStdCmdId;
			nxType.byte[1] = g_mbPollTab[i + 1].pollPortNameIdx;
			nxType.byte[2] = g_mbPollTab[i + 1].mbDevAddr;
		}

		if (curType.lword != nxType.lword)
		{
			nxStartAddr = i + 1;
			cnt = i - curStartAddr + 1;
			sortFlag = 1;
		}

		if (sortFlag)
		{
			for (m = 0; m < cnt - 1; m++)
			{
				for (j = curStartAddr; j < cnt - 1 - m + curStartAddr; j++)
				{
					if (g_mbPollTab[j].stdMbReadCmd > g_mbPollTab[j + 1].stdMbReadCmd)
					{
						temp = g_mbPollTab[j];
						g_mbPollTab[j] = g_mbPollTab[j + 1];
						g_mbPollTab[j + 1] = temp;
					}
				}
			}
		}
		curStartAddr = nxStartAddr;
	}
}
static void SortByMbDevAddr(void)
{
	INT16U m, i, j;
	MBPOLLCFG temp;
	INT16U curStartAddr = 0;
	INT16U nxStartAddr = 0;
	INT16U cnt = 0, sortFlag;

	lWord4Byte curType, nxType;

	for (i = 0; i < g_mbPollTabLen; i++)
	{
		sortFlag = 0;
		curType.lword = 0;
		nxType.lword = 0;
		curType.byte[0] = g_mbPollTab[i].nonStdCmdId;
		curType.byte[1] = g_mbPollTab[i].pollPortNameIdx;
		if (i <= g_mbPollTabLen - 1)
		{
			nxType.byte[0] = g_mbPollTab[i + 1].nonStdCmdId;
			nxType.byte[1] = g_mbPollTab[i + 1].pollPortNameIdx;
		}
		if (curType.lword != nxType.lword)
		{
			nxStartAddr = i + 1;
			cnt = i - curStartAddr + 1;
			sortFlag = 1;
		}
		if (sortFlag)
		{
			for (m = 0; m < cnt - 1; m++)
			{
				for (j = curStartAddr; j < cnt - 1 - m + curStartAddr; j++)
				{
					if (g_mbPollTab[j].mbDevAddr > g_mbPollTab[j + 1].mbDevAddr)
					{
						temp = g_mbPollTab[j];
						g_mbPollTab[j] = g_mbPollTab[j + 1];
						g_mbPollTab[j + 1] = temp;
					}
				}
			}
		}
		curStartAddr = nxStartAddr;
	}
}
static void SortByMbStartAddr(void)
{
	INT16U m, i, j;
	MBPOLLCFG temp;
	INT16U curStartAddr = 0;
	INT16U nxStartAddr = 0;
	INT16U cnt = 0, sortFlag;
	lWord4Byte curType, nxType;

	for (i = 0; i < g_mbPollTabLen; i++)
	{
		sortFlag = 0;
		curType.lword = 0;
		nxType.lword = 0;
		curType.byte[0] = g_mbPollTab[i].nonStdCmdId;
		curType.byte[1] = g_mbPollTab[i].pollPortNameIdx;
		curType.byte[2] = g_mbPollTab[i].mbDevAddr;
		curType.byte[3] = g_mbPollTab[i].stdMbReadCmd;
		if (i <= g_mbPollTabLen - 1)
		{
			nxType.byte[0] = g_mbPollTab[i + 1].nonStdCmdId;
			nxType.byte[1] = g_mbPollTab[i + 1].pollPortNameIdx;
			nxType.byte[2] = g_mbPollTab[i + 1].mbDevAddr;
			nxType.byte[3] = g_mbPollTab[i + 1].stdMbReadCmd;
		}
		if (curType.lword != nxType.lword)
		{
			nxStartAddr = i + 1;
			cnt = i - curStartAddr + 1;
			sortFlag = 1;
		}
		if (sortFlag)
		{
			for (m = 0; m < cnt - 1; m++)
			{
				for (j = curStartAddr; j < cnt - 1 - m + curStartAddr; j++)
				{
					if (g_mbPollTab[j].mbStartAddr > g_mbPollTab[j + 1].mbStartAddr)
					{
						temp = g_mbPollTab[j];
						g_mbPollTab[j] = g_mbPollTab[j + 1];
						g_mbPollTab[j + 1] = temp;
					}
				}
			}
		}
		curStartAddr = nxStartAddr;
	}
}

static void g_commPacketForm(void)
{
	INT16U m, i, j;
	MBPOLLCFG temp;
	INT16U curStartAddr = 0;
	INT16U nxStartAddr = 0;
	INT16U cnt = 0;
	INT8U sortFlag;
	lWord4Byte curType, nxType;

	if ((g_CommPacket = (PACKET_record *)malloc((g_mbPollTabLen) * sizeof(PACKET_record))) == NULL)
	{
		DebugPrint("malloc g_CommPacket error\n");
		exit(EXIT_FAILURE);
	}
	for (i = 0; i < g_mbPollTabLen; i++)
	{
		g_CommPacket[i].packetInex = 0xff;
		g_CommPacket[i].spCmdId = 0;
	}
	g_comPackeIdx = 0;
	for (i = 0; i < g_mbPollTabLen; i++)
	{
		sortFlag = 0;
		curType.lword = 0;
		nxType.lword = 0;
		curType.byte[0] = g_mbPollTab[i].nonStdCmdId;
		curType.byte[1] = g_mbPollTab[i].pollPortNameIdx;
		curType.byte[2] = g_mbPollTab[i].mbDevAddr;
		curType.byte[3] = g_mbPollTab[i].stdMbReadCmd;
		if (i <= g_mbPollTabLen - 1)
		{
			nxType.byte[0] = g_mbPollTab[i + 1].nonStdCmdId;
			nxType.byte[1] = g_mbPollTab[i + 1].pollPortNameIdx;
			nxType.byte[2] = g_mbPollTab[i + 1].mbDevAddr;
			nxType.byte[3] = g_mbPollTab[i + 1].stdMbReadCmd;
		}
		if (curType.lword != nxType.lword)
		{
			nxStartAddr = i + 1;
			cnt = i - curStartAddr + 1;
			sortFlag = 1;
		}
		if (sortFlag)
		{
			if (g_mbPollTab[curStartAddr].len < 2 && g_mbPollTab[curStartAddr].nonStdCmdId == 0)
			{
				PollBits_PacketTreat(curStartAddr, cnt);
			}
			else if (g_mbPollTab[curStartAddr].len == 2 && g_mbPollTab[curStartAddr].nonStdCmdId == 0)
			{
				PollWords_PacketTreat(curStartAddr, cnt);
			}
			else if (0 != g_mbPollTab[curStartAddr].nonStdCmdId)
			{
				PollSpecial_PacketTreat(curStartAddr, cnt);
			}
		}
		curStartAddr = nxStartAddr;
	}
	printf("1-------------------------------------------------\n");
	for (i = 0; i < g_mbPollTabLen; i++)
	{
		if (g_CommPacket[i].packetInex == 0xff)
			break;
		printf("g_index=%d ", g_CommPacket[i].packetInex);
		printf("portIdx=%d ", g_CommPacket[i].portIdx);
		printf("starAddrIndex=%d ", g_CommPacket[i].starAddrIndex);
		printf("spCmdId=%d ", g_CommPacket[i].spCmdId);
		printf("recLen=%d ", g_CommPacket[i].recLen);
		printf("sendLen=%d ", g_CommPacket[i].sendLen);
		printf("devAddr=%x ", g_CommPacket[i].content.devAddr);
		printf("cmd=%x ", g_CommPacket[i].content.cmd);
		printf("starMbAddr=%x ", g_CommPacket[i].content.starMbAddr);
		printf("byteSum=%x ", g_CommPacket[i].content.byteSum);
		printf("crc=%x \n", g_CommPacket[i].content.crc);
	}
	printf("2-------------------------------------------------\n");
}
//只读BIT组包处理
void PollBits_PacketTreat(INT16U starAddrIdx, INT16U len)
{
	INT16U i, j, temp, starAddr;
	MBPOLLCFG *pPtr;
	INT16U nxStarAddrIdx;
	const unsigned char mask[8] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80};

	pPtr = &g_mbPollTab[starAddrIdx];
	nxStarAddrIdx = 0;
	for (i = 0; i < len; i++)
	{
		g_CommPacket[g_comPackeIdx].packetInex = g_comPackeIdx;
		g_CommPacket[g_comPackeIdx].starAddrIndex = starAddrIdx + nxStarAddrIdx;
		g_CommPacket[g_comPackeIdx].content.cmd = pPtr[nxStarAddrIdx].stdMbReadCmd;
		g_CommPacket[g_comPackeIdx].content.starMbAddr = pPtr[nxStarAddrIdx].mbStartAddr;
		g_CommPacket[g_comPackeIdx].portIdx = pPtr[nxStarAddrIdx].pollPortNameIdx;
		g_CommPacket[g_comPackeIdx].content.devAddr = pPtr[nxStarAddrIdx].mbDevAddr;

		for (j = 1; j < (POLLPACKETMAXLEN + 1) * 8; j++)
		{
			pPtr[nxStarAddrIdx + j - 1].comPacketIndex = g_comPackeIdx;
			temp = pPtr[nxStarAddrIdx + j - 1].mbStartAddr - pPtr[nxStarAddrIdx].mbStartAddr;
			pPtr[nxStarAddrIdx + j - 1].byteIndex = temp / 8 + 4;
			pPtr[nxStarAddrIdx + j - 1].bitIndex = mask[temp % 8];

			if ((nxStarAddrIdx + j) >= len)
				break;
			if ((pPtr[nxStarAddrIdx + j].mbStartAddr - pPtr[nxStarAddrIdx].mbStartAddr) >= (POLLPACKETMAXLEN * 8))
				break;
			//if((pPtr[nxStarAddrIdx+j].mbStartAddr-pPtr[nxStarAddrIdx].mbStartAddr)>=pPtr[len-1].mbStartAddr)break;
			else
			{
			};
		}
		temp = pPtr[nxStarAddrIdx + j - 1].mbStartAddr - pPtr[nxStarAddrIdx].mbStartAddr + 1;
		g_CommPacket[g_comPackeIdx].recLen = (int)ceil((float)temp / 8) + 5;
		g_CommPacket[g_comPackeIdx].content.byteSum = temp;
		g_CommPacket[g_comPackeIdx].sendLen = 8;
		//--------------------------------------------------------------------------------------------------
		g_CommPacket[g_comPackeIdx].content.byteSum = htons(g_CommPacket[g_comPackeIdx].content.byteSum);
		g_CommPacket[g_comPackeIdx].content.starMbAddr = htons(g_CommPacket[g_comPackeIdx].content.starMbAddr);
		//g_CommPacket[g_comPackeIdx].content.crc=modbusCrc16(&g_CommPacket[g_comPackeIdx].content,6);
		temp = modbusCrc16((INT8U *)&g_CommPacket[g_comPackeIdx].content, 6);
		 g_CommPacket[g_comPackeIdx].content.crc = htons(temp);
		// g_CommPacket[g_comPackeIdx].content.crc = temp;
		//---------------------------------------------------------------------------------------------------
		nxStarAddrIdx += j;
		g_comPackeIdx++;
		if (nxStarAddrIdx >= len)
			break;
	}
}
void PollWords_PacketTreat(INT16U starAddrIdx, INT16U len)
{
	INT16U i, j, temp, starAddr;
	MBPOLLCFG *pPtr;
	INT16U nxStarAddrIdx;

	pPtr = &g_mbPollTab[starAddrIdx];

	nxStarAddrIdx = 0;
	for (i = 0; i < len; i++)
	{
		g_CommPacket[g_comPackeIdx].packetInex = g_comPackeIdx;
		g_CommPacket[g_comPackeIdx].starAddrIndex = starAddrIdx + nxStarAddrIdx;
		g_CommPacket[g_comPackeIdx].content.cmd = pPtr[nxStarAddrIdx].stdMbReadCmd;
		g_CommPacket[g_comPackeIdx].content.starMbAddr = pPtr[nxStarAddrIdx].mbStartAddr;
		g_CommPacket[g_comPackeIdx].portIdx = pPtr[nxStarAddrIdx].pollPortNameIdx;
		g_CommPacket[g_comPackeIdx].content.devAddr = pPtr[nxStarAddrIdx].mbDevAddr;

		for (j = 1; j < (POLLPACKETMAXLEN + 1); j++)
		{
			pPtr[nxStarAddrIdx + j - 1].comPacketIndex = g_comPackeIdx;
			temp = pPtr[nxStarAddrIdx + j - 1].mbStartAddr - pPtr[nxStarAddrIdx].mbStartAddr;
			pPtr[nxStarAddrIdx + j - 1].byteIndex = temp * 2 + 4;
			pPtr[nxStarAddrIdx + j - 1].bitIndex = 0xff;

			if ((nxStarAddrIdx + j) >= len)
				break;
			//if ((pPtr[nxStarAddrIdx + j].mbStartAddr - pPtr[nxStarAddrIdx].mbStartAddr) >= POLLPACKETMAXLEN)
			if ((pPtr[nxStarAddrIdx + j].mbStartAddr - pPtr[nxStarAddrIdx].mbStartAddr) >= 2)
				break;
			if ((pPtr[nxStarAddrIdx + j].mbStartAddr - pPtr[nxStarAddrIdx].mbStartAddr) > pPtr[len - 1].mbStartAddr)
				break;
			else
			{
			};
		}
		g_CommPacket[g_comPackeIdx].content.byteSum = pPtr[nxStarAddrIdx + j - 1].mbStartAddr - pPtr[nxStarAddrIdx].mbStartAddr + 1;
		g_CommPacket[g_comPackeIdx].recLen = 5 + g_CommPacket[g_comPackeIdx].content.byteSum * 2;
		g_CommPacket[g_comPackeIdx].sendLen = 8;
		//--------------------------------------------------------------------------------------------------
		g_CommPacket[g_comPackeIdx].content.byteSum = htons(g_CommPacket[g_comPackeIdx].content.byteSum);
		g_CommPacket[g_comPackeIdx].content.starMbAddr = htons(g_CommPacket[g_comPackeIdx].content.starMbAddr);
		temp = modbusCrc16((INT8U *)&g_CommPacket[g_comPackeIdx].content, 6);
		g_CommPacket[g_comPackeIdx].content.crc = htons(temp);
		// g_CommPacket[g_comPackeIdx].content.crc = temp;
		//---------------------------------------------------------------------------------------------------
		nxStarAddrIdx += j;
		g_comPackeIdx++;
		if (nxStarAddrIdx >= len)
			break;
	}
}

void PollSpecial_PacketTreat(INT16U starAddrIdx, INT16U len)
{
	INT16U i, j, temp, starAddr;
	MBPOLLCFG *pPtr;

	pPtr = &g_mbPollTab[starAddrIdx];
	g_CommPacket[g_comPackeIdx].packetInex = g_comPackeIdx;
	g_CommPacket[g_comPackeIdx].portIdx = pPtr[0].pollPortNameIdx;
	g_CommPacket[g_comPackeIdx].starAddrIndex = starAddrIdx;
	g_CommPacket[g_comPackeIdx].spCmdId = pPtr[0].nonStdCmdId;

	g_CommPacket[g_comPackeIdx].sendLen = 0;
	for (j = 0; j < g_nonStdMbCmdTabLen; j++)
	{
		if (pPtr[0].nonStdCmdId == g_nonStdMbCmdPacket[j].id)
		{
			g_CommPacket[g_comPackeIdx].sendLen = g_nonStdMbCmdPacket[j].sendLen;
			g_CommPacket[g_comPackeIdx].recLen = g_nonStdMbCmdPacket[j].recLen;
			break;
		}
	}
	for (i = 0; i < len; i++)
	{
		pPtr[i].comPacketIndex = g_comPackeIdx;
		pPtr[i].byteIndex = pPtr[i].mbStartAddr + g_nonStdMbCmdPacket[j].paraGetStartAddr;
		pPtr[i].bitIndex = 0xff;
	}
	g_comPackeIdx++;
}

static void tianYiYunDevData(void)
{
	sqlite3 *pdb = NULL;
	char **pResult;
	int nRow, nCol, nResult;
	char *errmsg;
	int i;

	nResult = sqlite3_open_v2(dbPathName, &pdb, SQLITE_OPEN_READWRITE, NULL);
	if (nResult)
	{
		fprintf(stderr, "can't open database:%s\n", sqlite3_errmsg(pdb));
		sqlite3_close(pdb);
		exit(EXIT_FAILURE);
	}
	char *strSql = "select * from tianYiYunDevData";
	nResult = sqlite3_get_table(pdb, strSql, &pResult, &nRow, &nCol, &errmsg);
	if (nResult != SQLITE_OK)
	{
		sqlite3_close(pdb);
		sqlite3_free(errmsg);
		return;
	}

	int nIndex = nCol;

	g_tabLen = nRow;

	if ((g_devDataTab = (devDataTable *)malloc((g_tabLen) * sizeof(devDataTable))) == NULL)
	{

		DebugPrint("malloc g_devDataTab error\n");
		exit(EXIT_FAILURE);
	}

	for (i = 0; i < g_tabLen; i++)
	{
		g_devDataTab[i].oid = atoi(pResult[nIndex]);
		strcpy(g_devDataTab[i].oidName, pResult[nIndex + 1]);
		strcpy(g_devDataTab[i].ssType, pResult[nIndex + 2]);
		strcpy(g_devDataTab[i].ssTypeCnName, pResult[nIndex + 3]);
		strcpy(g_devDataTab[i].ssDataType, pResult[nIndex + 4]);
		strcpy(g_devDataTab[i].ssDevId, pResult[nIndex + 5]);

		g_devDataTab[i].valueInt = atoi(pResult[nIndex + 6]);
		g_devDataTab[i].valueIntPre = g_devDataTab[i].valueInt;
		g_devDataTab[i].upSentPeriod = atoi(pResult[nIndex + 7]);
		g_devDataTab[i].dataOption = atoi(pResult[nIndex + 8]);

		g_devDataTab[i].belongToOid = atoi(pResult[nIndex + 9]);
		g_devDataTab[i].belongOidIdx = atoi(pResult[nIndex + 10]);
		g_devDataTab[i].radio = atoi(pResult[nIndex + 11]);
		nIndex = nIndex + nCol;
	}
	sqlite3_close(pdb);
	sqlite3_free(errmsg);
}

char *jxJsonForm(char *key, char *value)
{
	int m;
	cJSON *jsonRoot = NULL;
	cJSON *js_data = NULL;
	char *p = NULL;
	jsonRoot = cJSON_CreateObject();
	if (jsonRoot == NULL)
		goto end;
	cJSON_AddStringToObject(jsonRoot, key, value);
	p = cJSON_PrintUnformatted(jsonRoot);

end:
	cJSON_Delete(jsonRoot);
	printf(p);
	return p;
}
char *generalJsonForm(char *key1, char *value1, char *key2, char *value2)
{
	int m;
	cJSON *jsonRoot = NULL;
	cJSON *js_data = NULL;
	char *p = NULL;
	jsonRoot = cJSON_CreateObject();
	if (jsonRoot == NULL)
		goto end;
	cJSON_AddStringToObject(jsonRoot, key1, value1);
	cJSON_AddStringToObject(jsonRoot, key2, value2);
	p = cJSON_PrintUnformatted(jsonRoot);

end:
	cJSON_Delete(jsonRoot);
	printf(p);
	return p;
}
static void getInputCfg(void)
{
	char **pResult;
	int nRow, nCol, nResult;
	char *errmsg;
	int i;

	sqlite3 *pdb = NULL;
	nResult = sqlite3_open_v2(dbPathName, &pdb, SQLITE_OPEN_READWRITE, NULL);
	char *strSql = "select * from CfgTab_IO_DI";
	nResult = sqlite3_get_table(pdb, strSql, &pResult, &nRow, &nCol, &errmsg);
	if (nResult != SQLITE_OK)
		goto end;
	int nIndex = nCol;
	for (i = 0; i < nRow && i < IO_DI_MAXLEN; i++)
	{
		//g_IOinput[i].idx = changInputIdx(pResult[nIndex]);
		char tmpIdx = changInputIdx(pResult[nIndex]);
		if (tmpIdx >= IO_DI_MAXLEN)
			continue;
		g_IOinput[tmpIdx].option = atoi(pResult[nIndex + 1]);
		g_IOinput[tmpIdx].oid = atoi(pResult[nIndex + 2]);
		g_IOinput[tmpIdx].value = 0;
		nIndex = nIndex + nCol;
	}
	// for (i = 0; i < nRow && i < IO_DI_MAXLEN; i++)
	// {
	// 	//printf("g_IOinput[i].idx=%d ",g_IOinput[i].idx);
	// 	printf("option=%d ", g_IOinput[i].option);
	// 	printf("oid=%d ", g_IOinput[i].oid);
	// 	printf("\n ");
	// }
end:
	sqlite3_close(pdb);
	sqlite3_free(errmsg);
}
static void getOutputCfg(void)
{
	char **pResult;
	int nRow, nCol, nResult;
	char *errmsg;
	int i;

	sqlite3 *pdb = NULL;
	nResult = sqlite3_open_v2(dbPathName, &pdb, SQLITE_OPEN_READWRITE, NULL);
	char *strSql = "select * from CfgTab_IO_DO";
	nResult = sqlite3_get_table(pdb, strSql, &pResult, &nRow, &nCol, &errmsg);
	if (nResult != SQLITE_OK)
		goto end;
	int nIndex = nCol;

	for (i = 0; i < nRow && i < IO_DO_MAXLEN; i++)
	{
		char tmpIdx = changOutputIdx(pResult[nIndex]);
		if (tmpIdx >= IO_DO_MAXLEN)
			continue;
		//g_IOoutput[tmpIdx].idx = tmpIdx;
		g_IOoutput[tmpIdx].option = atoi(pResult[nIndex + 1]);
		g_IOoutput[tmpIdx].option &= 1;
		g_IOoutput[tmpIdx].oid = atoi(pResult[nIndex + 2]);
		get_jxOidValue(g_IOoutput[tmpIdx].oid, &g_IOoutput[tmpIdx].value);
		nIndex = nIndex + nCol;
	}
	// for (i = 0; i < nRow && i < IO_DO_MAXLEN; i++)
	// {
	// 	//	printf("g_IOoutput[i].idx=%d ", g_IOoutput[i].idx);
	// 	printf("option=%d ", g_IOoutput[i].option);
	// 	printf("oid=%d ", g_IOoutput[i].oid);
	// 	printf("value=%d ", g_IOoutput[i].value);
	// 	printf("\n ");
	// }
end:
	sqlite3_close(pdb);
	sqlite3_free(errmsg);
}
int changInputIdx(char *p)
{
	INT16U i;
	char *inputName[] = {"IN_1", "IN_2", "IN_3", "IN_4", "IN_5", "IN_6", "IN_7",
						 "IN_8", "IN_9", "IN_10", "IN_11", "IN_12", "IN_13", "IN_14", "IN_15", "IN_16"};

	for (i = 0; i < 16; i++)
	{
		if (!strcmp(p, inputName[i]))
			return i;
	}
	return 0;
}
int changOutputIdx(char *p)
{
	INT16U i;
	char *outName[] = {"OUT_1", "OUT_2", "OUT_3", "OUT_4", "OUT_5", "OUT_6", "OUT_7","OUT_8", };

	for (i = 0; i < 8; i++)
	{
		if (!strcmp(p, outName[i]))
			return i;
	}
	return 0;
}
static void getJxLinkageCfg(void)
{
	char **pResult;
	int nRow, nCol, nResult;
	char *errmsg;
	int i;

	sqlite3 *pdb = NULL;
	nResult = sqlite3_open_v2(dbPathName, &pdb, SQLITE_OPEN_READWRITE, NULL);

	char *strSql = "select * from jxLinkageTab";
	nResult = sqlite3_get_table(pdb, strSql, &pResult, &nRow, &nCol, &errmsg);
	if (nResult != SQLITE_OK)
		goto end;

	int nIndex = nCol;
	g_jxLinkageTabLen = nRow;
	//---------------------------------------------------

	if ((g_jxLinkageTab = (JXLINKAGETAB *)malloc(g_jxLinkageTabLen * sizeof(JXLINKAGETAB))) == NULL)
	{
		DebugPrint("malloc g_jxLinkageTab error\n");
		sleep(1);
		exit(EXIT_FAILURE);
	}

	for (i = 0; i < g_jxLinkageTabLen; i++)
	{
		g_jxLinkageTab[i].id = atoi(pResult[nIndex]);
		g_jxLinkageTab[i].oid_actDevice = atoi(pResult[nIndex + 1]);
		g_jxLinkageTab[i].linkageType = atoi(pResult[nIndex] + 3);
		g_jxLinkageTab[i].linkageLoopPeriod = atoi(pResult[nIndex] + 4) * 3600;
		g_jxLinkageTab[i].linkageStartTime = atoi(pResult[nIndex] + 5) * 60;
		g_jxLinkageTab[i].linkageKeepTime = atoi(pResult[nIndex] + 6) * 60;
		g_jxLinkageTab[i].oid_triggerDevice = atoi(pResult[nIndex + 7]);
		g_jxLinkageTab[i].actBitMask = 1;
		g_jxLinkageTab[i].value = 0;
		g_jxLinkageTab[i].status = IDLE;
		g_jxLinkageTab[i].startActionTime = 0;
		g_jxLinkageTab[i].curTime = 0;

		nIndex = nIndex + nCol;
	}

	JXLINKAGETAB temp;
	int j;
	for (i = 0; i < g_jxLinkageTabLen; i++)
	{
		for (j = 0; j < g_jxLinkageTabLen - i - 1; j++)
		{
			if (g_jxLinkageTab[j].oid_actDevice > g_jxLinkageTab[j + 1].oid_actDevice)
			{
				temp = g_jxLinkageTab[j];
				g_jxLinkageTab[j] = g_jxLinkageTab[j + 1];
				g_jxLinkageTab[j + 1] = temp;
			}
		}
	}
	int actBitMask = 1;
	for (i = 0; i < g_jxLinkageTabLen - 1; i++)
	{
		if (g_jxLinkageTab[i].oid_actDevice != g_jxLinkageTab[i + 1].oid_actDevice)
		{
			actBitMask = 1;
			continue;
		}
		g_jxLinkageTab[i].actBitMask = 1 << (actBitMask - 1);
		actBitMask++;
		g_jxLinkageTab[i + 1].actBitMask = 1 << (actBitMask - 1);
	}
	printf("\n--g_jxLinkageTab-------------------------------------\n");
	for (i = 0; i < g_jxLinkageTabLen; i++)
	{
		printf("id=%d ", g_jxLinkageTab[i].id);
		printf("oid_actDevice=%d ", g_jxLinkageTab[i].oid_actDevice);
		printf("linkageType=%d ", g_jxLinkageTab[i].linkageType);
		printf("linkageLoopPeriod=%d ", g_jxLinkageTab[i].linkageLoopPeriod);
		printf("linkageStartTime=%d ", g_jxLinkageTab[i].linkageStartTime);
		printf("linkageKeepTime=%d ", g_jxLinkageTab[i].linkageKeepTime);
		printf("oid_triggerDevice=%d ", g_jxLinkageTab[i].oid_triggerDevice);
		printf("actBitMask=%d\n ", g_jxLinkageTab[i].actBitMask);
	}

	printf("------g_jxLinkageTab end--------------------------------\n");
end:
	sqlite3_close(pdb);
	sqlite3_free(errmsg);
}